package sword_offer;

public class _03FindRepeatNumber {
    public static void main(String[] args) {
        int[] nums = {2, 3, 1, 0, 2, 5, 3};
        int repeatNumber = new _03FindRepeatNumber().findRepeatNumber(nums);
        System.out.println(repeatNumber);
    }

    public int findRepeatNumber(int[] nums) {
        final int LENGTH = nums.length;
        int index = 0;
        while (index < LENGTH){
            while (index != nums[index]){
                // 处理下标和对应位置的值不想等的情况：交换然后继续
                if(nums[index] == nums[nums[index]]){
                    return nums[index];
                }
                swap(index, nums[index], nums);
            }
            index++;
        }
        return -1;
    }

    public void swap(int indexA, int indexB, int[] array){
        int temp = array[indexA];
        array[indexA] = array[indexB];
        array[indexB] = temp;
    }
}
